package com.kun;

import org.junit.Test;
import redis.clients.jedis.Jedis;

//测试redis的ZSet数据结构
public class Test05_ZSet {


    @Test
    public void testZSet() {
        Jedis jedis = JedisUtils.getJedis();
        jedis.flushDB();

        //1. 创建ZSet
        jedis.zadd("zSet1", 10, "apple");
        jedis.zadd("zSet1", 8, "banana");
        jedis.zadd("zSet1", 15, "orange");
        System.out.println(jedis.zrange("zSet1", 0, -1)); // [banana, apple, orange]正序,小到大
        System.out.println(jedis.zrevrange("zSet1", 0, -1));// [orange, apple, banana]逆序,大到小
        System.out.println(jedis.zcard("zSet1"));//返回ZSet的元素个数

        //2. 通过score排序
        System.out.println(jedis.zrangeByScoreWithScores("zSet1", Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY));//按照score正序排序
        System.out.println(jedis.zrevrangeByScoreWithScores("zSet1", Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY));//按照score逆序排序

        //3. 移除元素
        jedis.zrem("zSet1", "banana");
        System.out.println(jedis.zrange("zSet1", 0, -1)); // [banana, apple, orange]正序,小到大

        //4. 按照区间计算元素个数
        System.out.println(jedis.zcount("zSet1", 11, 15)); // 1

        JedisUtils.closeJedis(jedis);


    }
}
